﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_ORDER.BizLogic;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_ORDER.OrderObject.UserControls;
using ERP_RORZE_PERMISSION.Base;
using MakerHelper = ERP_RORZE_ORDER.BizLogic.MakerHelper;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_ORDER.OrderObject.Forms
{
    public partial class frm_ProductEdit : DevExpress.XtraEditors.XtraForm
    {
        private ucProduct parent;

        public string ProductID;
        public string ProductName;
        public string EnglishName;
        public string VietName;
        public string JapanName;
        public string Size;
        public string MakerID;
        public string ProductGroupID;
        public string SupplierID;
        public string KindID;
        public string UnitID;
        public decimal Price;
        public string CurrencyID;
        private LogUtils log;
        public string PurposeID;

        public frm_ProductEdit(ucProduct ucP)
        {
            InitializeComponent();
            parent = ucP;
            this.CancelButton = this.btCancel;
            log = new LogUtils();
            this.ActiveControl = txtProductID;
        }

        private void frm_ProductAdd_Load(object sender, EventArgs e)
        {
            BindData();
            BindDataMaker();
            BindDataProductGroup();
            BindDataSupplier();
            BindDataKind();
            BindDataUnit();
            BindDataCurency();
            BindDataPurpose();
        }


        private void btEdit_Click(object sender, EventArgs e)
        {
            ProductHelper helper = new ProductHelper(new Product());
            if (GetData(helper.ValueObject))
            {

                if (helper.Update() > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.F2Order.Product;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "ProductID: " + helper.ValueObject.TempId + "; ProductName: " + helper.ValueObject.ProductName;
                    log.WriteLog();

                    if (Utilities.MessageConfirmationYesNo(OrderConstants.Message.UpdateSuccessAndQuestion))
                    {

                        txtProductID.Focus();
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(OrderConstants.Message.AddFailed);
                }
                parent._action = 0;
                parent.BindDatatoGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        #region Private Method
        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private bool GetData(Product o)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(txtProductID.Text.Trim()))
            {
                MessageBox.Show(OrderConstants.Message.Product.ProductIDCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtProductName.Text.Trim()))
            {
                MessageBox.Show(OrderConstants.Message.Product.ProductNameCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            //if (Utilities.IsNullOrEmpty(txtSize.Text.Trim()))
            //{
            //    MessageBox.Show(OrderConstants.Message.Product.SizeCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}

            if (Utilities.IsNullOrEmpty(cmbMaker.SelectedValue) || cmbMaker.SelectedItem == cmbMaker.Items[0])
            {
                MessageBox.Show(OrderConstants.Message.Product.MakerCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbProductGroup.SelectedValue) || cmbProductGroup.SelectedItem == cmbProductGroup.Items[0])
            {
                MessageBox.Show(OrderConstants.Message.Product.ProductGroupCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbSupplier.SelectedValue) || cmbSupplier.SelectedItem == cmbSupplier.Items[0])
            {
                MessageBox.Show(OrderConstants.Message.Product.SupplierCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbKind.SelectedValue) || cmbKind.SelectedItem == cmbKind.Items[0])
            {
                MessageBox.Show(OrderConstants.Message.Product.KindrCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbUnit.SelectedValue) || cmbUnit.SelectedItem == cmbUnit.Items[0])
            {
                MessageBox.Show(OrderConstants.Message.Product.UnitCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtPrice.Text.Trim()))
            {
                MessageBox.Show(OrderConstants.Message.Product.PriceCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbCurrency.SelectedValue) || cmbCurrency.SelectedItem == cmbCurrency.Items[0])
            {
                MessageBox.Show(OrderConstants.Message.Product.CurrencyCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbPurpose.SelectedValue) || cmbPurpose.SelectedItem == cmbPurpose.Items[0])
            {
                MessageBox.Show(OrderConstants.Message.Product.PurposeCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (decimal.Parse(txtPrice.Text.Trim()) <= 0)
            {
                MessageBox.Show(OrderConstants.Message.Product.PriceMustGreaterZero, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            //if (CheckDuplicate(txtProductID.Text.Trim()))
            //{
            //    MessageBox.Show(OrderConstants.Message.Product.ProductHasExisted, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}

            #endregion

            o.TempId = txtProductID.Text;
            o.ProductID = ProductID;
            o.ProductName = txtProductName.Text;
            o.EnglishName = txtEnglishlname.Text;
            o.VietName = txtVietName.Text;
            o.JapanName = txtJapanName.Text;
            o.Size = txtSize.Text;
            o.MakerID = cmbMaker.SelectedValue.ToString();
            o.ProductGroupID = cmbProductGroup.SelectedValue.ToString();
            o.SupplierID = cmbSupplier.SelectedValue.ToString();
            o.KindID = cmbKind.SelectedValue.ToString();
            o.UnitID = cmbUnit.SelectedValue.ToString();
            o.CurrencyID = cmbCurrency.SelectedValue.ToString();
            o.PurposeID = cmbPurpose.SelectedValue.ToString();
            o.Price = decimal.Parse(txtPrice.Text);

            return true;
        }

        private void BindData()
        {
            txtProductID.Text = ProductID;
            txtProductName.Text = ProductName;
            txtEnglishlname.Text = EnglishName;
            txtVietName.Text = VietName;
            txtJapanName.Text = JapanName;
            txtSize.Text = Size;
            txtPrice.Text = Price.ToString();
        }

        private void BindDataMaker()
        {
            MakerHelper helper = new MakerHelper(new Maker());
            DataTable dt = helper.GetAll(OrderConstants.StoreProcedure.Maker.GetAllFullName);
            DataRow row = dt.NewRow();

            row[OrderConstants.Entities.Maker.FieldName.FullName] = string.Empty;
            row[OrderConstants.Entities.Maker.FieldName.MakerID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbMaker.DataSource = dt;
            cmbMaker.DisplayMember = OrderConstants.Entities.Maker.FieldName.FullName;
            cmbMaker.ValueMember = OrderConstants.Entities.Maker.FieldName.MakerID;

            cmbMaker.SelectedValue = MakerID;
            if (cmbMaker.SelectedItem == null)
            {
                cmbMaker.SelectedItem = cmbMaker.Items[0];
            }

        }

        private void BindDataProductGroup()
        {
            ProductGroupHelper helper = new ProductGroupHelper(new ProductGroup());
            DataTable dt = helper.GetAll(OrderConstants.StoreProcedure.ProductGroup.GetAllFullName);
            DataRow row = dt.NewRow();

            row[OrderConstants.Entities.ProductGroup.FieldName.FullName] = string.Empty;
            row[OrderConstants.Entities.ProductGroup.FieldName.GroupID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbProductGroup.DataSource = dt;
            cmbProductGroup.DisplayMember = OrderConstants.Entities.ProductGroup.FieldName.FullName;
            cmbProductGroup.ValueMember = OrderConstants.Entities.ProductGroup.FieldName.GroupID;

            cmbProductGroup.SelectedValue = ProductGroupID;
            if (cmbProductGroup.SelectedItem == null)
            {
                cmbProductGroup.SelectedItem = cmbProductGroup.Items[0];
            }

        }

        private void BindDataSupplier()
        {
            SupplierHelper helper = new SupplierHelper(new Supplier());
            DataTable dt = helper.GetAll(OrderConstants.StoreProcedure.Supplier.GetAllFullName);
            DataRow row = dt.NewRow();

            row[OrderConstants.Entities.Supplier.FieldName.FullName] = string.Empty;
            row[OrderConstants.Entities.Supplier.FieldName.SupplierID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbSupplier.DataSource = dt;
            cmbSupplier.DisplayMember = OrderConstants.Entities.Supplier.FieldName.FullName;
            cmbSupplier.ValueMember = OrderConstants.Entities.Supplier.FieldName.SupplierID;

            cmbSupplier.SelectedValue = SupplierID;
            if (cmbSupplier.SelectedItem == null)
            {
                cmbSupplier.SelectedItem = cmbSupplier.Items[0];
            }

        }

        private void BindDataKind()
        {
            KindHelper helper = new KindHelper(new Kind());
            DataTable dt = helper.GetAll(OrderConstants.StoreProcedure.Kind.GetAllFullName);
            DataRow row = dt.NewRow();

            //row[OrderConstants.Entities.Kind.FieldName.FullName] = string.Empty;
            row[OrderConstants.Entities.Kind.FieldName.KindID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbKind.DataSource = dt;
            cmbKind.DisplayMember = OrderConstants.Entities.Kind.FieldName.KindID;
            cmbKind.ValueMember = OrderConstants.Entities.Kind.FieldName.KindID;

            cmbKind.SelectedValue = KindID;
            if (cmbKind.SelectedItem == null)
            {
                cmbKind.SelectedItem = cmbKind.Items[0];
            }

        }

        private void BindDataUnit()
        {
            UnitHelper helper = new UnitHelper(new Unit());
            DataTable dt = helper.GetAll(OrderConstants.StoreProcedure.Unit.GetAllFullName);
            DataRow row = dt.NewRow();

            //row[OrderConstants.Entities.Unit.FieldName.FullName] = string.Empty;
            row[OrderConstants.Entities.Unit.FieldName.UnitID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbUnit.DataSource = dt;
            cmbUnit.DisplayMember = OrderConstants.Entities.Unit.FieldName.UnitID;
            cmbUnit.ValueMember = OrderConstants.Entities.Unit.FieldName.UnitID;

            cmbUnit.SelectedValue = UnitID;
            if (cmbUnit.SelectedItem == null)
            {
                cmbUnit.SelectedItem = cmbUnit.Items[0];
            }

        }

        private void BindDataCurency()
        {
            CurrencyHelper helper = new CurrencyHelper(new Currency());
            DataTable dt = helper.GetAll(OrderConstants.StoreProcedure.Currency.GetAllFullName);
            DataRow row = dt.NewRow();

            row[OrderConstants.Entities.Currency.FieldName.FullName] = string.Empty;
            row[OrderConstants.Entities.Currency.FieldName.CurrencyID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbCurrency.DataSource = dt;
            cmbCurrency.DisplayMember = OrderConstants.Entities.Currency.FieldName.FullName;
            cmbCurrency.ValueMember = OrderConstants.Entities.Currency.FieldName.CurrencyID;

            cmbCurrency.SelectedValue = CurrencyID;
            if (cmbCurrency.SelectedItem == null)
            {
                cmbCurrency.SelectedItem = cmbCurrency.Items[0];
            }

        }

        private void BindDataPurpose()
        {
            PurposeHelper helper = new PurposeHelper(new Purpose());
            DataTable dt = helper.GetAll(OrderConstants.StoreProcedure.Purpose.GetAllFullName);
            DataRow row = dt.NewRow();

            row[OrderConstants.Entities.Purpose.FieldName.FullName] = string.Empty;
            row[OrderConstants.Entities.Purpose.FieldName.PurposeID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbPurpose.DataSource = dt;
            cmbPurpose.DisplayMember = OrderConstants.Entities.Purpose.FieldName.FullName;
            cmbPurpose.ValueMember = OrderConstants.Entities.Purpose.FieldName.PurposeID;

            cmbPurpose.SelectedValue = PurposeID;
            if (cmbPurpose.SelectedItem == null)
            {
                cmbPurpose.SelectedItem = cmbPurpose.Items[0];
            }

        }

        private bool CheckDuplicate(string productId)
        {
            ProductHelper helper = new ProductHelper(new Product());
            helper.ValueObject.ProductID = productId;
            DataTable dt = helper.GetOne();
            if (dt.Rows != null && dt.Rows.Count > 0)
                return true;
            return false;

        }
        #endregion

        private void frm_ProductEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.D)
                {
                    this.btEdit.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.End)
            {
                btEdit.Focus();
            }
            else if (e.KeyCode == Keys.Home)
            {
                txtProductID.Focus();
            }
        }


    }
}